package com.ruoyi.controller;

import com.ruoyi.domain.Audit;
import com.ruoyi.enumutil.AuditEnum;
import com.ruoyi.service.AuditService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.domain.Audit;
import com.ruoyi.service.AuditService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： AuditController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/UserSubmit")
public class UserSubmitController {

    @Resource
    private AuditService auditService;

    @Resource
    private SysUserMapper sysUserMapper;
    //查询
    @PostMapping("/get/Audit")
    public AjaxResult getAudit(@RequestBody Audit audit){
        //创建分页对象
        Page<Audit> page = new Page<>();
        //设置页码
        page.setCurrent(page.getCurrent());
        //设置行数
        page.setSize(page.getSize());
        //执行查询
        System.out.println("分页对象：" + page.getCurrent() + "---" + page.getSize());
        auditService.page(page, new LambdaQueryWrapper<Audit>()
                .like(StringUtils.isNotBlank(audit.getContent()),Audit::getContent,audit.getContent())
                .eq(Objects.nonNull(audit.getUserId()),Audit::getUserId, audit.getUserId())
                .eq(Objects.nonNull(audit.getAuditStatus()),Audit::getAuditStatus, audit.getAuditStatus())
                .eq(Audit::getUserId,SecurityUtils.getUserId())
                //逻辑删除
                .eq(Audit::getDeleteFlag, Boolean.FALSE)
        );

        page.getRecords().forEach(ele->{
            //判断用户编号不为 null
            if (Objects.nonNull(ele.getUserId())){
                //查询用户
                SysUser sysUser = sysUserMapper.selectUserById(ele.getUserId());
                //设置用户
                if (Objects.nonNull(sysUser)) ele.setSysUser(sysUser);
            }
        });


        //返回结果
        return AjaxResult.success(page);
    }

    //增加
    @PostMapping("/insert/Audit")
    public AjaxResult insertAudit(@RequestBody Audit audit){
        //设置默认值
        audit.setCreateBy(SecurityUtils.getUsername());
        audit.setCreateTime(new Date());
        audit.setUpdateBy(SecurityUtils.getUsername());
        audit.setUpdateTime(new Date());
        audit.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        auditService.save(audit);
        //返回结果
        return AjaxResult.success("新增成功");
    }
    /*组管理员
        用户提交审核*/
    //修改
    @PostMapping("/update/Audit")
    public AjaxResult updateAudit(@RequestBody Audit audit){
        //设置默认值
        audit.setUpdateTime(new Date());
        audit.setUpdateBy(SecurityUtils.getUsername());
        //设置状态为待审核
        audit.setAuditStatus(AuditEnum.DaiShenHe.getAuditStatus());
        //调用Service层删除
        boolean update = auditService.updateById(audit);
        //返回结果
        return update == Boolean.TRUE ? AjaxResult.success("申诉成功") : AjaxResult.error("申诉失败");
    }

}
